import { Injectable } from '@angular/core';
import { Router ,RouteParams} from '@angular/router-deprecated';
import { BaseRequestOptions,Http,Response, HTTP_PROVIDERS,Headers,URLSearchParams} from '@angular/http';
import { GlobalService } from './global.service';
import { BaseService } from './base.service';
import { Api } from '../model/api.model';

@Injectable()
export class ApiService extends BaseService
{
  headers:Headers;
  constructor(private http:Http,private router:Router,private globalService:GlobalService) {
    super();
  }

  add(api:Api)
  {
    this.headers.set("x-token",this.globalService.getUser().token);
    return this.http.post(BaseService.apiUrl+'/docs',JSON.stringify(api),{headers:this.headers});
  }

  update(api:Api)
  {
    this.headers.set("x-token",this.globalService.getUser().token);
    return this.http.put(BaseService.apiUrl+'/docs/'+api._id,JSON.stringify(api),{headers:this.headers});
  }

  deleteApi(apiId:string)
  {
    this.headers.set("x-token",this.globalService.getUser().token);
    return this.http.delete(BaseService.apiUrl+'/docs/'+apiId,{headers:this.headers});
  }

  get(apiId:string)
  {
    if(this.globalService.getUser())
    {
      this.headers.set("x-token",this.globalService.getUser().token);
      return this.http.get(BaseService.apiUrl+'/docs/'+apiId,{headers:this.headers});
    }
  }

  getApis(groupId:string)
  {
    var options = new BaseRequestOptions();
    var params = new URLSearchParams();
    params.append("groupId",groupId);
    options.search = params;
    return this.http.get('http://localhost:8000/kapi/apis',options);
  }

  getApisByProj(projId:string)
  {
    if(this.globalService.getUser())
    {
      this.headers.set("x-token",this.globalService.getUser().token);
      return this.http.get(BaseService.apiUrl+'/docs/project/'+projId,{headers:this.headers});
    }
  }
}
